#include "gameselect.h"
#include "gamehall.h"
#include "gameroom.h"
#include <QIcon>
#include <QPushButton>
#include <QSound>
#include <QPainter>
#include <QFont>
#include <QTextEdit>
#include <QFile>
#include <QTextStream>

GameSelect::GameSelect(QWidget *parent) : QWidget(parent)
{
    this->setFixedSize(1500,1200); // 设置窗口大小
    this->setWindowIcon(QIcon(":res/res/ico.png"));
    this->setWindowTitle("游戏关卡选择");

    QPushButton *backBtn=new QPushButton(this);
    backBtn->move(1300,1100);
    backBtn->setIcon(QIcon(":res/res/back.png")); // 设置按钮图标

    // 点击返回按钮返回到主界面
    connect(backBtn,&QPushButton::clicked,[=](){
        this->close();
        GameHall *gameHall=new GameHall;
        gameHall->show();

        QSound::play(":res/res/clicked.wav");
    });

    QFont font("华文琥珀",24);

    GameRoom *gameRoom=new GameRoom;

    // 这是启动简单模式的按钮
    QPushButton *simpleBtn=new QPushButton(this);
    simpleBtn->move(610,200);
    simpleBtn->setText("简单模式");
    simpleBtn->setFont(font);
    connect(simpleBtn,&QPushButton::clicked,[=](){
       this->close();
        gameRoom->setGeometry(this->geometry());
        gameRoom->show();

        QSound::play(":res/res/clicked.wav");

        gameRoom->setTimeout(300);
    });

    // 这是启动正常模式的按钮
    QPushButton *normalBtn=new QPushButton(this);
    normalBtn->move(610,350);
    normalBtn->setText("正常模式");
    normalBtn->setFont(font);
    connect(normalBtn,&QPushButton::clicked,[=](){
        this->close();
        gameRoom->setGeometry(this->geometry());
        gameRoom->show();

        QSound::play(":res/res/clicked.wav");

        gameRoom->setTimeout(200);
    });

    // 这是启动困难模式的按钮
    QPushButton *hardBtn=new QPushButton(this);
    hardBtn->move(610,500);
    hardBtn->setText("困难模式");
    hardBtn->setFont(font);
    connect(hardBtn,&QPushButton::clicked,[=](){
        this->close();
        gameRoom->setGeometry(this->geometry());
        gameRoom->show();

        QSound::play(":res/res/clicked.wav");

        gameRoom->setTimeout(100);
    });

    // 这是启动历史战绩的按钮
    QPushButton *hisBtn=new QPushButton(this);
    hisBtn->move(610,650);
    hisBtn->setText("历史战绩");
    hisBtn->setFont(font);
    connect(hisBtn,&QPushButton::clicked,[=](){
        QWidget *widget=new QWidget;
        widget->setWindowTitle("历史战绩");
        widget->setFixedSize(500,300);

        QTextEdit *edit=new QTextEdit(widget);
        edit->setFont(font);
        edit->setFixedSize(500,300);

        QFile file("D:\Qt\qt\greedy_snake\Snake\history.txt");
        file.open(QIODevice::ReadOnly);

        QTextStream in(&file);
        int data=in.readLine().toInt();

        edit->append("得分为:");
        edit->append(QString::number(data));

        widget->show();
    });
}

void GameSelect::paintEvent(QPaintEvent *event)
{
    // 实例化画家对象
    QPainter painter(this);

    QPixmap pix(":res/res/game_select.png");

    painter.drawPixmap(0,0,this->width(),this->height(),pix);
}





